# coding:utf-8
from threading import Thread
from project import mail
from flask import current_app
from flask_mail import Message


def send(context,msg):
    with context:
        mail.send(msg)


def create_message(subject, body, sender, recipients):
    msg = Message(subject=subject, sender=sender, recipients=recipients)
    msg.body = body.encode('utf-8')
    return msg

def send_email(subject, body, sender='no-reply@uniyes.com', recipients=[]):
    msg = create_message(subject=subject, body=body, sender=sender, recipients=recipients)
    context = current_app.app_context()
    thr = Thread(target=send,args=[context,msg])
    thr.start()
